package dp;

public class dp188 {
    public static void main(String[] args) {
        System.out.println(maxProfit(2, new int[]{}));
    }
    public static int maxProfit(int k, int[] prices) {
        if (prices.length == 0 || prices == null)
            return 0;
        int n = prices.length;
        int[][] dp = new int[n][2 * k + 1];
        for (int i = 1; i <= k; i++) {
            dp[0][2 * i - 1] = -prices[0];
        }
        for (int i = 1; i < n; i++) {
            dp[i][0] = dp[i - 1][0];
            for (int j = 1; j <= k; j++) {
                dp[i][j * 2 -1] = Math.max(dp[i - 1][j * 2 - 2] - prices[i], dp[i - 1][j * 2 -1]);
                dp[i][j * 2] = Math.max(dp[i - 1][j * 2 - 1] + prices[i], dp[i - 1][j * 2]);
            }
        }
        int m = 0;
        for (int i = 1; i <= k; i++) {
            m = Math.max(m, dp[n - 1][i * 2]);
        }
        return m;
    }
}
